package com.xr.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xr.common.constant.MiniProgramConstants;
import com.xr.miniprogram.domain.MiniProgramComment;
import com.xr.miniprogram.domain.MiniProgramOrderUser;
import com.xr.miniprogram.domain.MiniProgramServiceLog;
import com.xr.miniprogram.domain.vo.MiniProgramCommentResultVO;
import com.xr.miniprogram.mapper.MiniProgramCommentMapper;
import com.xr.miniprogram.mapper.MiniProgramOrderUserMapper;
import com.xr.miniprogram.mapper.MiniProgramServiceLogMapper;
import com.xr.miniprogram.service.IMiniProgramCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.List;

import static com.xr.common.utils.PageUtils.startPage;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author da
 * @since 2023-09-12
 */
@Service
public class IMiniProgramCommentServiceImpl extends ServiceImpl<MiniProgramCommentMapper, MiniProgramComment> implements IMiniProgramCommentService {

    @Resource
    private MiniProgramCommentMapper commentMapper;
    @Resource
    private MiniProgramOrderUserMapper orderUserMapper;
    @Resource
    private MiniProgramServiceLogMapper serviceLogMapper;

    @Override
    public MiniProgramCommentResultVO selectRate(Long serviceId) {
        int goodTotal = 0, midTotal = 0, badTotal = 0;
        /**
         * 查询好评中评差评的数量
         */
        List<MiniProgramComment> miniProgramComments = commentMapper.selectList(new QueryWrapper<MiniProgramComment>().lambda().eq(MiniProgramComment::getServiceId, serviceId));
        Iterator<MiniProgramComment> iterator = miniProgramComments.iterator();
        while (iterator.hasNext()) {
            MiniProgramComment miniProgramComment = iterator.next();
            Long rate = miniProgramComment.getRate();
            if (rate <= 0) {
                iterator.remove();
            } else {
                if(rate > MiniProgramConstants.COMMENT_STANDARD) ++goodTotal;
                else if(rate < MiniProgramConstants.COMMENT_STANDARD) ++badTotal;
                else ++midTotal;

            }
        }
        int total = goodTotal + midTotal + badTotal;
        return total == 0 ? new MiniProgramCommentResultVO(goodTotal, midTotal, badTotal, total, (double) 0) : new MiniProgramCommentResultVO(goodTotal, midTotal, badTotal, total, Double.valueOf(new DecimalFormat("#.0000").format(goodTotal*1.0 / total)));
    }


    @Override
    public List<MiniProgramComment> selectByServiceIdAndTopic(Long serviceId, Long topic) {
        return commentMapper.selectByServiceIdAndTopic(serviceId, topic);
    }

    @Override
    public MiniProgramComment selectByOrderUserId(Long orderUserId) {
        MiniProgramOrderUser miniProgramOrderUser = orderUserMapper.selectOne(new QueryWrapper<MiniProgramOrderUser>().lambda().eq(MiniProgramOrderUser::getOrderUserId, orderUserId));
        MiniProgramServiceLog miniProgramServiceLog = serviceLogMapper.selectById(miniProgramOrderUser.getServiceLogId());
        return this.getById(miniProgramServiceLog.getMiniProgramCommentId());
    }

}
